package de.bazinga.rotw.network;

import com.captiveimagination.jgn.synchronization.SyncObjectManager;
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.captiveimagination.jgn.synchronization.message.SynchronizeRemoveMessage;
import com.jme.scene.Node;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.gameModes.CaptureTheZombie;
import de.bazinga.rotw.network.message.SynchronizeCreatePlayerMessage;
import de.bazinga.rotw.network.message.ZombieInfoMessage;
import de.bazinga.rotw.person.OpponentList;
import de.bazinga.rotw.person.Person;
import de.bazinga.rotw.util.NetworkConstants;
import de.bazinga.rotw.weapon.portal.PortalGun;

public class BasicSyncManager implements SyncObjectManager
{
	private Node scene;
	
	//customizable
	//Objects which get synchronized for server and client
	//private Box playerBox;
	//customizable
	/**
	 * creates a new object if a create message was recieved
	 * and attaches it to the node of the game.
	 */
	public Object create(SynchronizeCreateMessage scm)
	{
		System.out.println("Create Message");
		
		if(scm.getSyncObjectType() == NetworkConstants.OBJ_PLAYER) {
			// Create a new Player
			Person person = new Person(
					((SynchronizeCreatePlayerMessage)scm).getPlayerName(),
					((SynchronizeCreatePlayerMessage)scm).getModelTexture(),
					((SynchronizeCreatePlayerMessage)scm).getTeam());
			person.getController().setSpeed(1f);
			
			System.out.println("PLAYER ADDED: "+scm.getPlayerId());
			OpponentList.getInstance().addOpponent(scm.getPlayerId(),person);
			person.setPlayerId(scm.getPlayerId());
			
			Person.updateAllTeamBoxes();
			
			
			person.createBodyPhysicsNode(scene);
			
			//scene.attachChild(person);
			scene.updateRenderState();

			if(((RotwGameState)GameStateManager.getInstance().getChild("gameState")).isServer()) {
				BasicSyncManager syncManager = 
					((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getSyncManager();
				((Server)syncManager).updateScore();
			}
			
			RotwGameState gameState = (RotwGameState)GameStateManager.getInstance().getChild("gameState");
			
			if(((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getPlayer().hasZombie()) {
				if(((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getPlayer().getTeam() == Person.TEAM_ONE) {
					((CaptureTheZombie)gameState.getGameMode()).getZombie(1)
						.sendInfoMessage(ZombieInfoMessage.ZOMBIE_CAUGHT);
				}else{
					((CaptureTheZombie)gameState.getGameMode()).getZombie(0)
						.sendInfoMessage(ZombieInfoMessage.ZOMBIE_CAUGHT);
				}
			}
			
			return person;
		}
		
		return null;
		
	}

	//customizable
	/**
	 * removes an object if a remove message was recieved
	 * and removes it from its parent.
	 */
	public boolean remove(SynchronizeRemoveMessage srm, Object obj) {
		System.out.println("Remove Message");
		if(obj instanceof Person) {
			if(((Person)obj).hasZombie()) {
				
				RotwGameState gameState = (RotwGameState)GameStateManager.getInstance().getChild("gameState");
				
				int otherZombieIndex = (((Person)obj).getTeam() == Person.TEAM_ONE) ? 1:0;
				((CaptureTheZombie)gameState.getGameMode()).getZombie(otherZombieIndex).reset();
				
			}
			
			if(((Person)obj).hasVehicle()) {
				((Person)obj).getVehicle().setCurrentlyUsed(false);
			}
			
			OpponentList.getInstance().removeOpponent(((Person) obj).getPlayerId());
			((RotwGameState)GameStateManager.getInstance().getChild("gameState")).addSpatialToRemove(
					((PortalGun)((Person)obj).getWeapon(PortalGun.class)).getEntry());
			((RotwGameState)GameStateManager.getInstance().getChild("gameState")).addSpatialToRemove(
					((PortalGun)((Person)obj).getWeapon(PortalGun.class)).getExit());
			((RotwGameState)GameStateManager.getInstance().getChild("gameState")).addSpatialToRemove((Person)obj);


			if(((RotwGameState)GameStateManager.getInstance().getChild("gameState")).isServer()) {
				BasicSyncManager syncManager = 
					((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getSyncManager();
				((Server)syncManager).updateScore();
			}
			
			return true;
		}
		return false;
	}
	
	public void setScene(Node scene) 
	{
		this.scene = scene;
	}

}
